package cn.scw.project.separate.controller;

import cn.scw.common.constant.SysConsts;
import cn.scw.common.separate.SeparateMethod;
import cn.scw.common.separate.model.FenZhangMercSetModel;
import cn.scw.framework.aspectj.lang.annotation.Log;
import cn.scw.framework.aspectj.lang.enums.BusinessType;
import cn.scw.framework.web.controller.BaseController;
import cn.scw.framework.web.domain.AjaxResult;
import cn.scw.framework.web.page.TableDataInfo;
import cn.scw.project.separate.domain.SaUser;
import cn.scw.project.separate.domain.SaUserImg;
import cn.scw.project.separate.service.SaUserImgService;
import cn.scw.project.separate.service.SaUserService;
import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Api(tags = "分账用户信息")
@RestController
@RequestMapping(SysConsts.CONTEXT_PATH + "/separate/user")
public class SeparateController extends BaseController {

    @Resource
    private SaUserService saUserService;

    @Resource
    private SaUserImgService saUserImgService;

    @Resource
    private SeparateMethod separateMethod;

    @ApiOperation("查询分账用户列表")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam Map<String, Object> params) {
        startPage();
        List<SaUser> list = saUserService.list(params);
        return getDataTable(list);
    }

    @ApiOperation("查询所有分账用户列表")
    @GetMapping("/list/all")
    public AjaxResult listAll(@RequestParam Map<String, Object> params) {
        List<SaUser> list = saUserService.list(params);
        return AjaxResult.success(list);
    }

    @ApiOperation("获取分账用户详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        SaUser saUser = saUserService.select(id);

        SaUserImg saUserImg = saUserImgService.selectByUserId(id);
        saUser.setSaUserImg(saUserImg);

        return AjaxResult.success(saUser);
    }

    @ApiOperation("新增分账用户")
    @Log(title = "分账用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(SaUser user) {
        Long id = saUserService.saveNew(user);
        return AjaxResult.success(id);
    }

    @ApiOperation("绑定分账账号")
    @Log(title = "绑定分账账号", businessType = BusinessType.INSERT)
    @PostMapping("/bind")
    public AjaxResult bind(SaUser user) {
        FenZhangMercSetModel setModel = new FenZhangMercSetModel();
        SaUser saUser = saUserService.select(user.getId());
        if (saUser == null || StringUtils.isBlank(saUser.getMercNo())) return AjaxResult.error("未生成随身付商户号或账号不存在");
        List<String> strings = Lists.newArrayList();
        strings.add(saUser.getMercNo());
        setModel.setMercListJsonStr(JSONArray.toJSONString(strings));
        Boolean isBind = separateMethod.SetSeparateAccount(setModel);
        if (!isBind) return AjaxResult.error("账号绑定失败");
        saUser.setIsBind(1);
        saUserService.save(saUser);
        return AjaxResult.success();
    }

    @ApiOperation("修改分账用户")
    @Log(title = "分账用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(SaUser user) {
        Long id = saUserService.save(user);
        return AjaxResult.success(id);
    }

    @ApiOperation("删除分账用户")
    @Log(title = "分账用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        saUserService.delete(ids);
        return AjaxResult.success();
    }

    @ApiOperation("获取分行信息")
    @Log(title = "获取分行信息")
    @GetMapping("/getlbk")
    public AjaxResult getlbk(String lbnkProv, String lbnkCity, String bnkCd, String banktype) {
        return AjaxResult.success(separateMethod.getlbkNo(lbnkProv, lbnkCity, bnkCd, banktype));
    }
}
